package com.github.binarylei.zkclient;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

import java.util.List;

/**
 * @author: leigang
 * @version: 2018-04-06
 */
public class ZkClientBase {

    private static final String CONNECT_ADDR = "127.0.0.1:2181";
    private static final int SEESION_OUTTIME = 5 * 1000;
    
    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), 10 * 1000);

        //1. create and delete
        zkClient.createEphemeral("/temp");
        zkClient.createPersistent("/super/c1", true);
        zkClient.deleteRecursive("/super");

        //2. 设置path和data，并且读取于节点和每个节点的内容
        zkClient.createPersistent("/super", "1234");
        zkClient.createPersistent("/super/c1", "c1");
        zkClient.createPersistent("/super/c2", "c2");

        List<String> list = zkClient.getChildren("/super");
        for (String path : list) {
            String rp = "/super/" + path;
            System.out.println(rp + "：" + zkClient.readData(rp).toString());
        }

        //3. 更新和判断节点是否存在
        zkClient.writeData("/super/c1", "新内容");
        System.out.println("/super/c1：" + zkClient.readData("/super/c1").toString());
        System.out.println("/super/c1：" + zkClient.exists("/super/c1"));
    }

}
